package com.zx.viewlearn.views.mypullrorefresh;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/2/11.
 * 注释：
 */

public class RefreshView extends RelativeLayout implements StateListener {
    ObjectAnimator mObjectAnimator;

    class ViewHolder {
        TextView text_view;
        ImageView loading_iv;

        public ViewHolder(View root) {
            this.text_view = root.findViewById(R.id.refresh_text_view);
            this.loading_iv = root.findViewById(R.id.loading_iv);
//            Log.e("RefreshView", "ViewHolder");
        }

        public void setTextStr(String text) {
            if (text_view != null) this.text_view.setText(text);
        }

        public void setImageSrc(int resId) {
            if (loading_iv != null) loading_iv.setImageResource(resId);
        }

        public void startLoading() {
            if (loading_iv != null) {
                if (mObjectAnimator == null) {
                    mObjectAnimator = ObjectAnimator.ofFloat(loading_iv, "rotation", 0, 360);
                    mObjectAnimator.setDuration(1000);
                    mObjectAnimator.setStartDelay(300);//延时加载，防止图片替换不成功
                    mObjectAnimator.setInterpolator(new LinearInterpolator());
                    mObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
                }
                mObjectAnimator.start();
            }

        }

        public void stopLoading() {
            if (loading_iv != null) {
                loading_iv.clearAnimation();
            }
            if (mObjectAnimator != null) {
                Log.e("RefreshView", "stopLoading");
                mObjectAnimator.cancel();
                mObjectAnimator.end();
            }

        }

        public void init() {
            if (loading_iv != null) {
                loading_iv.clearAnimation();
            }
        }
    }

    ViewHolder mViewHolder;


    public RefreshView(Context context) {
        super(context);
        init(context, null);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
    }

    private void initViews() {
        mViewHolder = new ViewHolder(this);
        mViewHolder.init();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        initViews();


    }

    @Override
    public void init() {
        mViewHolder.setImageSrc(R.drawable.pull_icon_big);
        mViewHolder.init();
        mViewHolder.setTextStr("下拉刷新");

    }

    @Override
    public void releaseToDo() {
        mViewHolder.setImageSrc(R.drawable.pullup_icon_big);
        mViewHolder.setTextStr("释放后刷新");
    }


    @Override
    public void doing() {
        //这边可以加载动画
        mViewHolder.setImageSrc(R.drawable.loading);
        mViewHolder.startLoading();
        mViewHolder.setTextStr("正在刷新");

    }

    @Override
    public void done() {
        //停止动画
        mViewHolder.setImageSrc(R.drawable.load_succeed);
        mViewHolder.stopLoading();
        mViewHolder.setTextStr("刷新完成");

    }


}
